jQuery(document).ready(function(){
	bindLikeAction();
});

function bindLikeAction(){
	jQuery(".goals .like").unbind('click').click(function(){
		var $like = jQuery(this);
		var id = $like.attr("id");
		var component = $like.attr("rel");
		var $likedCount = $like.next(".peoplelikenumber");
		
		if('GOAL_ENTRY' == component){
			if($like.hasClass('likeicon')){
				UserManagerExt.likeThisEntry(id, function(result){
					if(result == -1){
						//TODO: alert('cannot like');
					}else if(result == 1){
						$like.removeClass('likeicon');
						$like.addClass('likedicon');
						var number = parseInt($likedCount.html());
						$likedCount.html(number+1);
						
						// update like list
						if (number == 0) {
							jQuery("div#" + id + "_false_like").show();
						} else {
							var $like_list = jQuery("span[rel=" + id + "_false_random_list]");
							list = "Bạn, " + $like_list.html();
							$like_list.html(list);
						}
					}
				});
			}else if($like.hasClass('likedicon')){
				UserManagerExt.unlikeThisEntry(id, function(result){
					if(result == -1){
						//TODO: alert('cannot like');
					}else if(result == 1){
						$like.removeClass('likedicon');
						$like.addClass('likeicon');
						var number = parseInt($likedCount.html());
						$likedCount.html(number-1);
						
						// update like list
						if (number == 1) {
							jQuery("div#" + id + "_false_like").hide();
						} else {
							var $like_list = jQuery("span[rel=" + id + "_false_random_list]");
							list = $like_list.html().substring(5);
							$like_list.html(list);
						}
					}
				});
			}
		}else if('DONE_ENTRY' == component){
			if($like.hasClass('likeicon')){
				UserManagerExt.likeThisDoneEntry(id, function(result){
					if(result == -1){
						//TODO: alert('cannot like');
					}else if(result == 1){
						$like.removeClass('likeicon');
						$like.addClass('likedicon');
						var number = parseInt($likedCount.html());
						$likedCount.html(number+1);
						
						// update like list
						var $like_list = jQuery("span[rel=" + id + "_true_random_list]");
						list = "Bạn, " + $like_list.html();
						$like_list.html(list);
					}
				});
			}else if($like.hasClass('likedicon')){
				UserManagerExt.unlikeThisDoneEntry(id, function(result){
					if(result == -1){
						//TODO: alert('cannot like');
					}else if(result == 1){
						$like.removeClass('likedicon');
						$like.addClass('likeicon');
						var number = parseInt($likedCount.html());
						$likedCount.html(number-1);
						
						// update like list
						var $like_list = jQuery("span[rel=" + id + "_true_random_list]");
						list = $like_list.html().substring(5);
						$like_list.html(list);
					}
				});
			}
		}
		
		return false;
	});	
}

//function bindViewLikeListAction() {
//	jQuery("a[rel^='like_list_']").unbind("click").click(function() {
//		entryId = jQuery(this).attr("rel").substring(10);
//		viewLikeList(entryId, false);
//	});
//}

function viewLikeList(entryId, isEntryDone) {
	GoalManager.getEntryEntryLikeList(entryId, isEntryDone, 1, 1, function(result) {
		if (result != null) {
			list = "";
			for ( var i = 0; i < result.items.length; i++) {
				list = result.items[i].username + ", " + list;
			}
			alert(list);
		}
	});
}

function like(component, componentId) {
	var isLoginned = false;
	UserManagerExt.isLogined(function(re){
		isLoginned = re;
		LikeService.like(component, componentId, function(result) {
			if (result == 1) {
				var relAttr=component+"_"+componentId+"_unlike_button";
				var newRelAttr=component+"_"+componentId+"_like_button";
				// hide like button
				var $likeButton = jQuery("a.entryLikeThis[rel="+relAttr+"]");
				$likeButton.attr("href","javascript:unlike('"+component+"',"+componentId+")");
				$likeButton.attr("rel",newRelAttr);
				$likeButton.addClass("entryLikeThisSelected");			
				// update like list
				LikeService.getRandomLikeList(-1, componentId, component, function(result) {
					if (result.length > 0) {
						if(isLoginned){
							list = "Bạn";
						}else{
							list = result[0];
						}
						for (i = 1; i < result.length; i++) {
							username = result[i];
							if(isLoginned){
								if (username != currentUser) {
									list = list + ", " + username;
								}
							}
							else{
								list = list + ", " + username;
							}
						}
						LikeService.count(component, componentId, function(likeCount){
							numberLeft = likeCount - result.length;
							if (numberLeft > 0) {
								list = list + " và ";
								leftList = numberLeft + " người khác";
								jQuery("a[rel="+component+"_"+componentId+"_like_list]").html(leftList).fadeIn();
								jQuery("a[rel="+component+"_"+componentId+"_like_list]").fancybox({
									'scrolling': 'no'
								});
							}else{
								jQuery("a[rel="+component+"_"+componentId+"_like_list]").html("").hide();
							}
							jQuery("span[rel="+component+"_"+componentId+"_random_list]").html(list);
						});						
					}
				});
			}
		});
	});	
}

function unlike(component, componentId) {
	var isLoginned = false;
	LikeService.unlike(component, componentId, function(result) {
		if (result == 1) {
			var relAttr=component+"_"+componentId+"_like_button";
			var newRelAttr=component+"_"+componentId+"_unlike_button";
			// hide like button
			var $unlikeButton = jQuery("a.entryLikeThis[rel="+relAttr+"]");
			$unlikeButton.attr("href","javascript:like('"+component+"',"+componentId+")");
			$unlikeButton.attr("rel",newRelAttr);
			$unlikeButton.removeClass("entryLikeThisSelected");
			// update like list
			LikeService.getRandomLikeList(-1, componentId, component, function(result) {
				if (result.length > 0) {
					list = result[0];
					for (i = 1; i < result.length; i++) {
						username = result[i];
						list = list + ", " + username;
					}
					LikeService.count(component, componentId, function(likeCount){
						numberLeft = likeCount - result.length;
						if (numberLeft > 0) {
							list = list + " và ";
							leftList = numberLeft + " người khác";
							jQuery("a[rel="+component+"_"+componentId+"_like_list]").html(leftList).fadeIn();
							jQuery("a[rel="+component+"_"+componentId+"_like_list]").fancybox({
								'scrolling': 'no'
							});
						}else{
							jQuery("a[rel="+component+"_"+componentId+"_like_list]").html("").hide();
						}
						jQuery("span[rel="+component+"_"+componentId+"_random_list]").html(list);
					});						
				}
			});
		}
	});
}